<html>
<head><meta charset="utf-8"><title>Visibility for modules · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html">Visibility for modules</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="184434684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184434684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184434684">(Dec 29 2019 at 20:11)</a>:</h4>
<p>I could use the new visibility infra for the auto-import functionality <a href="https://github.com/rust-analyzer/rust-analyzer/issues/2180" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/2180">https://github.com/rust-analyzer/rust-analyzer/issues/2180</a><br>
But for that, I need to have the visibility data for modules at least (functions would be great to have either, but not that necessary for an initial auto import implementation).</p>
<p>It looks like I need to implement the <code>HasVisibility</code> trait for the <code>ra_hir::code_model::Module</code> struct.<br>
If I understand it correctly, currently only the <code>ra_hir_def::nameres::raw::ModuleData</code> contains the data I need, but I have no idea how to get it with the <code>ra_hir::code_model::Module</code> data.<br>
There's also <code>ra_hir_def::nameres::ModuleData</code> struct which I can retrieve with the <code>ra_hir::code_model::Module</code> data, but there's no visibility data in 2 out of 3 places where this object is constructed and I have no clue on how to get it.</p>
<p>So, any hints on how can I get that working?</p>



<a name="184434932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184434932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184434932">(Dec 29 2019 at 20:19)</a>:</h4>
<p>I think adding <code>HasVisibility</code> to <code>Module</code> would be the wrong approach for this. The visibility you're interested in should be that of a certain <em>path</em>, not the module itself. E.g.</p>
<div class="codehilite"><pre><span></span><span class="k">mod</span> <span class="nn">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">mod</span> <span class="nn">bar</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">foo</span>::<span class="n">bar</span><span class="p">;</span><span class="w"></span>
</pre></div>


<p>so in general, items can appear in many modules (everywhere they're <code>use</code>d) and have a different visibility in each of those places. I think to find a path to import a certain item, we need to consider each of those places and whether it's visible from where we're importing.</p>



<a name="184435010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435010">(Dec 29 2019 at 20:21)</a>:</h4>
<p>Btw, I was planning on working on this (finding paths to an item) to use it to qualify paths in assists, but I have to admit I haven't gotten to it yet</p>



<a name="184435125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435125">(Dec 29 2019 at 20:25)</a>:</h4>
<p>That is what we want in the end, right.</p>
<p>The issue with this approach currently is that we don't have any reexport infra at all (<a href="https://github.com/rust-analyzer/rust-analyzer/issues/2180#issuecomment-569218100" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/2180#issuecomment-569218100">https://github.com/rust-analyzer/rust-analyzer/issues/2180#issuecomment-569218100</a>) and the only information we have currently is the <code>ra_hir::code_model::Module</code> and an information on the parent modules that contain it.</p>
<p>So now we can implement a very limited version of the auto imports, for cases where all modules are publicly available hence my initial question.<br>
Or do you think we should go with the reexports instead?</p>



<a name="184435170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435170">(Dec 29 2019 at 20:26)</a>:</h4>
<p>the <code>resolve_module_path</code> method of <code>Resolver</code> returns a <code>PerNs</code> which contains the correct visibility for the end of the path. the publically visible methods don't return that yet though. Also the visibility of the intermediate segments isn't considered yet (I'm not sure whether it should)</p>



<a name="184435185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435185">(Dec 29 2019 at 20:27)</a>:</h4>
<p>I would suggest you keep it as simple as possible at first ;)</p>



<a name="184435188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435188">(Dec 29 2019 at 20:27)</a>:</h4>
<p>even if it means that we'll import some wrong paths at first</p>



<a name="184435235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435235">(Dec 29 2019 at 20:28)</a>:</h4>
<p>ah wait, <code>resolve_module_path_in_items</code> is public and also returns the <code>PerNs</code></p>



<a name="184435368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435368">(Dec 29 2019 at 20:33)</a>:</h4>
<p>Yes, the easiest way would be great to have. <br>
The methods you've suggested seem cool, but a bit more complex compared to what I've expected indeed :)</p>
<p>I can get the <code>ra_syntax::ast::generated::Visibility</code> easily from the <code>ra_hir::code_model::Module</code>  with the  following snippet:</p>
<div class="codehilite"><pre><span></span>let visibility: Option&lt;Visibility&gt; = module
                        .declaration_source(db)
                        .and_then(|module_declaration| module_declaration.value.visibility());
</pre></div>


<p>That's why I hoped that the proper Visibility object is easy to get.</p>



<a name="184435416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435416">(Dec 29 2019 at 20:34)</a>:</h4>
<p>what exactly do you want to do currently? build a path to import and then check whether it's visible? in that case I would suggest resolving it and checking the returned visibility</p>



<a name="184435431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435431">(Dec 29 2019 at 20:35)</a>:</h4>
<p>I'm not sure we should add a 'get visibility' API to <code>Module</code> at all, because it'd often be the wrong thing to use</p>



<a name="184435474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435474">(Dec 29 2019 at 20:36)</a>:</h4>
<p>Yes, exactly, to check that every module in the <code>use foo::bar::baz</code> is visible from the place we add the import to.<br>
I'll try resolving it, thank you.</p>



<a name="184435530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435530">(Dec 29 2019 at 20:38)</a>:</h4>
<blockquote>
<p>is visible from the place we add the import to</p>
</blockquote>
<p>Just to clarify: under "visible" I mean the visibility from the module declaration, since we don't have the reexport support.</p>



<a name="184435578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435578">(Dec 29 2019 at 20:40)</a>:</h4>
<p>I personally think for a MVP of auto-import we wouldn't even need to do that -- not showing the assist if the path would not be visible vs. showing it and importing a 'wrong' (not visible) path is not that much of an improvement IMO, and in the future we will want to handle this differently (by generating a different path) anyway</p>



<a name="184435637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184435637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184435637">(Dec 29 2019 at 20:42)</a>:</h4>
<p>Makes sense, I guess.<br>
In that case I've got a hacky implementation that can add those imports already <span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span>  and all that's left is to refactor this mess.</p>



<a name="184436511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Visibility%20for%20modules/near/184436511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Visibility.20for.20modules.html#184436511">(Dec 29 2019 at 21:13)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="133169">@matklad</span> , I'll go with the laziest approach to start with</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>